<?php

class Book_AdminManageController extends Core_Controller_Action_Admin {

    public function indexAction() {

        //menu
        $this->view->navigation = $navigation = Engine_Api::_()->getApi('menus', 'core')
                ->getNavigation('book_admin_main', array(), 'book_admin_main_manage');

        //delete book
        if ($this->getRequest()->isPost()) {
            $values = $this->getRequest()->getPost();
            foreach ($values as $key => $value) {
                if ($key == 'delete_' . $value) {
                    $book = Engine_Api::_()->getItem('book', $value);
                    $book->delete();
                }
            }
        }

        //table books 
        $bookTb = Engine_Api::_()->getDbTable('books', 'book');

        //select book to display
        $select = $bookTb->select();

        //phan trang
        $this->view->books = $books = Zend_Paginator::factory($select);
        $page = $this->_getParam('page', 1);
        $books->setItemCountPerPage(10);
        $books->setCurrentPageNumber($page);
    }

    public function deleteAction() {
        //delete book
        $book_id = $this->_getParam('book_id');
        var_dump($book_id);

        if ($this->getRequest()->isPost()) {
            //
            $book = Engine_Api::_()->getItem('book', $book_id);
            $book->delete();

            //
            $this->_forward('success', 'utility', 'core', array(
                'smoothboxClose' => 10,
                'parentRefresh' => 10,
                'messages' => array('')
            ));
        }
    }

}
